package com.example.mvp.mvppromote.presenter;

import com.example.mvp.mvppromote.base.BasePresenter;
import com.example.mvp.mvppromote.model.WebViewArticle;
import com.example.mvp.mvppromote.network.Network;
import com.example.mvp.mvppromote.ui.iview.IWebView;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wutongyu on 16/6/24.
 */
public class WebViewPresenter extends BasePresenter<IWebView> {
    private IWebView iWebView;

    public WebViewPresenter(IWebView iWebView) {
        super(iWebView);
        this.iWebView = iWebView;
    }

    public void getWebUrl(String id) {
        showDialog(iWebView.getMyContext());
        Subscription subscription = Network.getWebViewApi().getGuokrArticle(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WebViewArticle>() {
                    @Override
                    public void onCompleted() {
                        closeDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(WebViewArticle webViewArticle) {
                        iWebView.loadUrl(webViewArticle);
                    }
                });
        addSubscribe(subscription);

    }
}
